package com.app.milkteaapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.milkteaapp.bean.FindUserGsonBean;
import com.app.milkteaapp.R;
import com.app.milkteaapp.bean.RechargeGsonBean;
import com.app.milkteaapp.utils.GsonUtils;
import com.app.milkteaapp.utils.OkHttpUtils;
import com.app.milkteaapp.utils.MyTools;
import com.app.milkteaapp.utils.SpUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RechargeActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_money;
    private Button btn_confirm;
    private String money;
    Context context = this;
    float money_ok;
    private int userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        initView();
    }

    private void initView() {
        et_money = (EditText) findViewById(R.id.et_money);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);

        btn_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
              cha();

                break;
        }
    }

    private void cha() {
        money = et_money.getText().toString().trim();
        //拿用户id
        userid = SpUtils.getUserBean(this).getId();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (TextUtils.isEmpty(money)) {
            Toast.makeText(this, "money不能为空", Toast.LENGTH_SHORT).show();
        }else {
            OkHttpUtils.MyCall(MyTools.URL+"/user/findById",jsonObject.toString()).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "网络炸了", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onResponse(@NotNull final Call call, @NotNull Response response) throws IOException {
                    //gson解析
                    String string = response.body().string();

                    final FindUserGsonBean findUserGsonBean = GsonUtils.fromJson(string,FindUserGsonBean.class);

                    if(findUserGsonBean.isSuccess()){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                float money_yue =  findUserGsonBean.getData().getMoney();

                                float moeny1 = Float.parseFloat(money);

                                money_ok= moeny1 + money_yue;
                                chong(money_ok);
                            }
                        });
                    }
                }
            });

        }
    }

    private void chong(float money) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id",userid);
            jsonObject.put("money", money);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        OkHttpUtils.MyCall(MyTools.URL + "/user/recharge",jsonObject.toString()).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "网络炸了", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string1 = response.body().string();
                RechargeGsonBean rechargeGsonBean = GsonUtils.fromJson(string1, RechargeGsonBean.class);

                if (rechargeGsonBean.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("_________", "run: ");
                            Toast.makeText(context, "充值成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });
                }
            }
        });
    }


}
